<input type='hidden' name='subquestions_sortorder' value='' />
<input type='hidden' id='subquestions_deletedqids' name='subquestions_deletedqids' value='' />

{% set first = true %}
{% set scale_id = 0 %}
{# TODO: Add question type attribute to indicate if the type supports relevance on subquestions #}
{% set supportsRelevance = question.questionType.code != 'H' %}

<!-- Loop all language, and then hide all but the current selected one with JS. -->
{% for lang in allLanguages %}
    <div class="lang-hide lang-{{ lang }} {{ first ? 'base-lang' : 'extra-lang' }}">
        {% for scale_id in range(0, scalecount - 1) %}
            {% set anscount = subquestions|length %}
            {# TODO : check the rendering of XSCALES / Y SCALES #}

            {% set position=0 %}
            {% if scalecount > 1 %}
                {% if scale_id==0 %}
                    <div class='header ui-widget-header'>
                        <i>{{ gT("Y-Scale") }}</i>
                    </div>
                {% else %}
                    <div class='header ui-widget-header'>
                        <i>{{ gT("X-Scale") }}</i>
                    </div>
                {% endif %}
            {% endif %}

            <!-- Subquestions table -->
            <!-- NB: Both questions and answer options uses class "answertable". -->
            <div class="subquestions-table-wrapper" style="max-height: 414px;">
                <table
                    class='answertable subquestions-table table table-responsive  {{ supportsRelevance ? '' : 'hide-relevance' }}'
                    id='subquestions_{{lang}}_{{scale_id}}'
                    data-scaleid='{{ scale_id }}'
                    data-input-data-id='add-subquestion-input-javascript-datas'
                >

                    <!-- Headers -->
                    <thead>
                        <tr>
                            <th></th>
                            <th class='col-lg-1'>{{ gT("Code") }}
                            <i
                class="ri-information-fill"
                data-bs-toggle="tooltip"
                title="{{ gT("A mandatory unique alpha-numeric code for each subquestion (1-20 characters)") }}"
            > </i>
                            </th>
                            <th>
                                {{ gT("Subquestion") }}
                                <i class="ri-earth-fill" data-bs-toggle="tooltip" title="{{ "This setting is localized" | gT }}"></i>
                            </th>
                            {% if first %}
                                {% if scale_id == 0 %}
                                    <th id='rel-eq-th' class='col-lg-1'>
                                        {{ gT("Relevance equation") }}
                                    </th>
                                {% endif %}
                                <th class="col-lg-1">
                                    {{ gT("Action") }}
                                </th>
                            {% endif %}
                        </tr>
                    </thead>

                    <!-- Body -->
                    <tbody>
                        {% for subquestion in subquestions[scale_id] %}
                            <!-- Line tag -->
                            {% include 'subquestionRow.twig' with {
                                'position'  : position,
                                'scale_id'  : scale_id,
                                'activated' : activated,
                                'first'     : first,
                                'subquestion': subquestion,
                                'subquestionl10n': subquestion.questionl10ns[lang],
                                'language'  : lang
                            } %}

                            {% set position = position + 1 %}

                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <div class="action-buttons" style="padding-bottom: 10px;">
                {% if activated == 'Y' %}
                    {% set disabled = 'disabled="disabled"' %}
                {% else %}
                    {% set disabled = '' %}
                {% endif %}

                <button
                    {{ disabled }}
                    data-scale-id="{{ scale_id }}"
                    class='btnlsbrowser btn btn-outline-secondary'
                    type='button'
                    onclick="LS.questionEditor.showLabelSetPicker(event, 'subquestions');"
                >
                    {{ gT('Load label set') }}
                </button>

                {% if hasLabelSetPermission %}
                    <button
                        class='bthsaveaslabel btn btn-outline-secondary'
                        id='bthsaveaslabel_{{ scale_id }}'
                        type='button'
                        onclick="LS.questionEditor.showLabelSetSaver(event, 'subquestions-table');"
                        data-bs-target="#saveaslabelModal"
                    >
                        {{ gT('Save label set') }}
                    </button>
                {% endif %}

                <button {{ disabled }} id='btnquickadd_{{ lang }}_{{ scale_id }}' data-scale-id="{{ scale_id }}" class='btn btn-outline-secondary btnquickadd float-end' type='button' data-bs-toggle="modal" data-bs-target="#quickaddModal" data-scale-id="{{ scale_id }}">
                    {{ gT('Quick add') }}
                </button>
            </div>
            {% set position=sprintf("%05d", position) %}
        {% endfor %}
    </div>
    {% set first=false %}
{% endfor %}

<!-- For javascript -->
<input
    type="hidden"
    id="add-subquestion-input-javascript-datas"
    data-url="{{ createUrl('/questionAdministration/getSubquestionRowForAllLanguages/') }}"
    data-quickurl="{{ createUrl(
        '/questionAdministration/getSubquestionRowQuickAdd',
        ({
            'surveyid' : question.sid,
            'gid' : question.gid
        })
    ) }}"
    data-assessmentvisible="{% if assessmentvisible==true %}1{% else %}0{% endif %}"
    data-errormessage="An error occured while processing the ajax request."
    data-languages='{{ json_encode(allLanguages) }}'
    data-surveyid="{{ question.sid }}"
    data-gid="{{ question.gid }}"
    data-qid="{{ question.qid }}"
    data-scale-id="{{ scale_id }}" {# -1 : because it's incremented via <  #}
/>
